/*******************************************************************************
**
** Copyright (c) 2011 - The OWE Team
**
** This file is part of OWE.
**
** OWE is a free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License as published by the Free Software Foundation; version
** version 2.1 of the License.
**
** OWE is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public
** License along with OWE. If not, see <http://www.gnu.org/licenses/>.
**
*******************************************************************************/

#include <owe/configparser.h>
#include <owe/configurationdocument.h>
#include <owe/ustring.h>
#include <owe/widgetelement.h>
#include <owe/widgetpackage.h>

#include "configurationdocument_p.h"

OWE_USE_NAMESPACE

/*!
    \class ConfigurationDocumentPrivate

    \skip
*/
ConfigurationDocumentPrivate::ConfigurationDocumentPrivate(const WidgetPackage &package)
    : valid(true),
      package(&package)
{
    if (!package.isValid()) {
        valid = false;
        return;
    }

    config = new ConfigParser(package.getAbsolutePath("config.xml"));
    if (!config->isValid())
        valid = false;
}

ConfigurationDocumentPrivate::~ConfigurationDocumentPrivate()
{
    delete config;
}

/*!
    \class ConfigurationDocument
    \brief Configuration document abstraction

    \ingroup Configuration

    This class represents a configuration document, as specified
    <a href="http://www.w3.org/TR/widgets/#configuration-document">here</a>.
*/

/*!
    Constructs a ConfigurationDocument by passing a valid \a package.
*/
ConfigurationDocument::ConfigurationDocument(const WidgetPackage &package) :
    d_ptr(new ConfigurationDocumentPrivate(package))
{
}

/*!
    Destructs the object releasing all allocated resources.
*/
ConfigurationDocument::~ConfigurationDocument()
{
    delete d_ptr;
}

/*!
    Returns if the configuration document is valid, by following the processing
    algorithm detailed at <a
    href="http://www.w3.org/TR/widgets/#algorithm-to-process-a-configuration-doc0">
    W3C specification</a>.
*/
bool ConfigurationDocument::isValid() const
{
    return d_ptr->valid;
}

/*!
    Returns a valid WidgetElement or 0, otherwise. The WidgetElement is the root
    of the ConfigurationDocument tree and actually the only mandatory element.
    If the ConfigurationDocument is valid, you can assume that the WidgetElement
    exists.

    \sa isValid()
*/
const WidgetElement *ConfigurationDocument::widgetElement() const
{
    return d_ptr->config->widgetElement();
}
